const Message = require('../../model/message')
const GroupMessage = require('../../model/groupmessage')
const Member = require('../../model/member')
const Group = require('../../model/group')

exports.user = async(uid,fid,res)=>{
    try {
        var data =await Message.find({$or:[{user:uid,send:fid},{user:fid,send:uid}]}).sort(-1).limit(1).select('-_id time type message')
    } catch (error) {
        return res.send({
          status:400
        })
    }
   if(data.length>0){
    res.send({
      status:200
    })
   }else{
    res.send({
      status:400
    })
   }
}

exports.group = async(gid,res)=>{
    try {
        var data =await GroupMessage.find({groupId:gid}).sort(-1).limit(1).select('-_id time message type')
    } catch (error) {
        return res.send({
          status:400
        })
    }
    if(data.length>0){
        res.send({
          status:200
        })
       }else{
        res.send({
          status:400
        })
       }
}

exports.delgroup = async (gid,res)=>{
  try {
    await Group.deleteOne({groupId:gid})
    await Member.deleteMany({groupId:gid})
  } catch (error) {
    return res.send({
      status:400
    })
  }
  res.send({
    status:200
  })
}